﻿using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XRepair.Models;
using RepositoryUtil.SQL;
using System.Collections.Generic;
using XRepair.Conventions.Enum;

namespace MvcXRepairCustomer.Repository.SQL
{
    public class ServiceDAO : BaseDAO
    {
        public IEnumerable<Service> GetAllActiveServices()
        {
            string spName = "GetAllActiveServices";

            DataTable resultTab = null;
            List<Service> serviceList = null;

            using (var conn = XRepairConn)
            {
                conn.Open();

                SqlCommand command = new SqlCommand(spName, conn);
                command.CommandType = CommandType.StoredProcedure;
                resultTab = command.ExecuteDataTable();
            }

            if (resultTab != null && resultTab.Rows.Count > 0)
            {
                serviceList = this.ConvertDataTableToServiceList(resultTab);
            }

            return serviceList;
        }

        private List<Service> ConvertDataTableToServiceList(DataTable data)
        {
            List<Service> services = null;

            if (data != null)
            {
                services = new List<Service>();

                foreach (DataRow dr in data.Rows)
                {
                    var serviceModel = new Service();

                    serviceModel.ID = SqlUtil<int>.GetValue(dr, "ID");
                    serviceModel.DeviceTypeID = (DeviceTypes)SqlUtil<int>.GetValue(dr, "GenericTypesID");
                    serviceModel.SymptomTypeID = SqlUtil<int>.GetValue(dr, "SymptomTypeID");
                    serviceModel.ServiceName = SqlUtil<string>.GetValue(dr, "ServiceName");
                    serviceModel.ServiceDescription = SqlUtil<string>.GetValue(dr, "ServiceDescription");
                    serviceModel.LaborCost = SqlUtil<double>.GetValue(dr, "LaborCost");
                    serviceModel.DiscountLabor = SqlUtil<double>.GetValue(dr, "DiscountLabor");
                    serviceModel.PartCost = SqlUtil<double>.GetValue(dr, "PartCost");
                    serviceModel.DiscountPart = SqlUtil<double>.GetValue(dr, "DiscountPart");

                    services.Add(serviceModel);
                }
            }

            return services;
        }
    }
}
